#include "tasktable.h"

taskTable *taskTable::fourQuadrantTables[4];

taskTable::taskTable(QTableWidget *table, abstractTable* parent): abstractTable(table, parent)
{
//    m_table = table;

    m_table->clear();
    m_table->setColumnCount(4);
    // 隐藏行号
    QHeaderView *header = m_table->verticalHeader();
    header->setHidden(true);
    header = m_table->horizontalHeader();
//    header->setSectionResizeMode(QHeaderView::Stretch);
    header->setSectionResizeMode(NAME, QHeaderView::ResizeToContents);
    m_table->setColumnWidth(ID, 35);
    m_table->setColumnHidden(ID, true);
    m_table->setColumnWidth(DELETE, 35);


    // 设置单元格无法选中
//    _table->setSelectionMode(QAbstractItemView::NoSelection);
    // 以行为单位选中
    m_table->setSelectionBehavior ( QAbstractItemView::SelectRows);
    m_table->setSelectionMode ( QAbstractItemView::SingleSelection);
//    qDebug() << "table "<<sizeof(*table);
    m_table->setFont(QFont("宋体"));
    //设置表格中每一行的表头
    m_table->setHorizontalHeaderLabels(QStringList() << "ID"<< "任务名" << "番茄数" << "操作");
    //设置表格数据区内的所有单元格都不允许编辑
    m_table->setEditTriggers(QAbstractItemView::NoEditTriggers);

    m_table->setShowGrid(false);
    m_table->setWindowFlags(Qt::FramelessWindowHint);

    QSize size(15,15);
    m_pix_trash = QPixmap(":/pics/delete.png").scaled(size);

}

//taskTable::taskTable(const taskTable &t)
//{
//    this->m_table = t.m_table;
//}

//taskTable::taskTable(taskTable &t)
//{
//    this->m_table = t.m_table;
//}

taskTable::taskTable(abstractTable* parent): abstractTable(parent)
{
    m_table = nullptr;
}

void taskTable::addTask(taskItem &task)
{
    int newRow = m_table->rowCount();
    m_table->insertRow(newRow);

    QTableWidgetItem * widget_item;
    qDebug() << task.m_taskName << task.m_eachDayTomatoes << task.m_todayAchieveTomato << endl;

    widget_item = new QTableWidgetItem(QString("%1").arg(task.m_id));
//    widget_item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    m_table->setItem(newRow,ID, widget_item);

    widget_item = new QTableWidgetItem(QString(task.m_taskName));
    widget_item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    m_table->setItem(newRow,NAME, widget_item);


    widget_item = new QTableWidgetItem(QIcon(":/pics/tomato.png"),QString("%2/%1")
                                       .arg(task.m_eachDayTomatoes).arg(task.todayAchieveTomatoes()));
    widget_item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);

    m_table->setItem(newRow,TOMATO, widget_item);

    // 删除按钮

    QLabel *trash = new QLabel();

    // 设置鼠标在label上的形状
    trash->setCursor(Qt::PointingHandCursor);
    trash->setPixmap(m_pix_trash);
    trash->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    m_table->setCellWidget(newRow, DELETE, trash);




}

void taskTable::removeTask(int row)
{
    m_table->removeRow(row);
}

void taskTable::updateTask(taskItem &task)
{

    int id;
    taskItem *old_task = taskItem::all_tasks.value(task.m_id);
    taskTable * table = taskTable::fourQuadrantTables[task.m_sort];
    if (old_task->m_sort == task.m_sort)
    {
        for(int r=0; r<table->m_table->rowCount();++r)
        {
            id = table->m_table->item(r, ID)->text().toInt();
            if (id == task.m_id)
            {
                table->m_table->item(r, NAME)->setText(task.m_taskName);
                table->m_table->item(r, TOMATO)->setText(QString("%2/%1").arg(task.m_eachDayTomatoes).arg(task.m_todayAchieveTomato));
                break;
            }

        }
    } else
    {
        // 删除旧列表中对应项
        taskTable * old_table = taskTable::fourQuadrantTables[old_task->m_sort];
        for(int r=0; r<old_table->m_table->rowCount();++r)
        {
            id = old_table->m_table->item(r, ID)->text().toInt();
            if (id == old_task->m_id)
            {
                old_table->removeTask(r);
                break;
            }

        }
        // 增加新列表中对应项
        table->addTask(task);

    }

    qDebug() << "taskTable update task " << "old sort " << old_task->m_sort << " new sort " << task.m_sort << " task name " << task.m_taskName;
}

int taskTable::getId(int row)
{
    if (row < 0 || row >= m_table->rowCount()) return -1;
    return m_table->item(row, ID)->text().toInt();


}

QString taskTable::getName(int row)
{
    if (row < 0 || row >= m_table->rowCount()) return "";
    return m_table->item(row, NAME)->text();
}
